﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using UTSDAL.LINQSQL.DAO;

namespace UTSDAL.MAIL
{
    public class SendMailClient
    {
        private static ParameterDAO parameterDAO = new ParameterDAO();

        public static void SendGenerationEmail()
        {

            String timetablerAdmin = parameterDAO.findParameterValue("TimetablerAdmin");
            String timetablerAdminAddress = parameterDAO.findParameterValue("TimetablerAdminAddress");
            String generationSubject = parameterDAO.findParameterValue("GenerationSubject");
            String generationBody = parameterDAO.findParameterValue("GenerationBody");
            String host = parameterDAO.findParameterValue("SMTPHost");

            short port = Convert.ToInt16(parameterDAO.findParameterValue("SMTPPort"));

            String username = parameterDAO.findParameterValue("SMTPUser");
            String password = parameterDAO.findParameterValue("SMTPPassword");

            SendMail(timetablerAdmin, timetablerAdminAddress, generationSubject, generationBody, host, port, username, password,true);

        }


        public static void SendMail(String to, String from, String subject, String body, String host, short port, String username, String password, Boolean ssl)
        {

            try
            {
                MailMessage mail = new MailMessage();

                mail.To.Add(to);

                mail.From = new MailAddress((from));

                mail.Subject = subject;

                mail.Body = body;

                mail.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient();
                smtp.Host = host;
                //smtp.Port = port;
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = new System.Net.NetworkCredential(username, password);

                smtp.EnableSsl = ssl;

                smtp.Send(mail);

            }
            catch (Exception exc)
            {
                String msg = exc.Message;
            }
        }
    }
}
